<?php

namespace App\Http\Controllers;

use App\Models\RefundDetail as RefundDetailModel;
use App\Jobs\RefundJob;
use D1M\Common\Helpers\Result;
use Illuminate\Http\Request;
use App\Http\Requests\RefundRequest;
use App\Http\Requests\RefundQueryRequest;
use App\Models\Refund as RefundModel;
use D1M\Common\Exceptions\BusinessException;

class RefundController extends Controller
{
    public function index(RefundRequest $request)
    {
        $params = $request->post('data');

        $refund = RefundModel::where('order_no', $params['order_no'])->where('refund_no', $params['refund_no'])->first();
        if (!$refund) {
            $refund = new RefundModel();
            $refund->order_no = $params['order_no'];
            $refund->refund_no = $params['refund_no'];
            $refund->refund_amount = $params['refund_amount'];
            $refund->balance_amount = $params['balance_amount'];
            $refund->platform = $params['platform'];

            $refund->save();
        } else {
            if ($refund->status !== 0) {
                throw new BusinessException('退款单号已经存在', -100);
            }
        }
        if ($params['refund_amount'] < $params['balance_amount']) {
            throw new BusinessException('退款总金额不能小于余额退款金额', -101);

        }
        //查询订单支付信息
        $payItem = $refund->getPayment($params['order_no']);
        if (!$payItem) {
            throw new BusinessException('没有可退款金额', -102);
        }

        $info = [
            'refunds_id' => $refund->id,
            'order_no' => $params['order_no'],
            'refund_no' => $params['refund_no'],
            'refund_amount' => $params['refund_amount'],
            'balance_amount' => $params['balance_amount'],
            'items' => $payItem
        ];
        //异步请求退款接口
        $this->dispatch(new RefundJob($info));


        return Result::success();
    }

    public function query(RefundQueryRequest $request)
    {
        $order_no = $request->post('data')['order_no'];
        $refund_no = $request->post('data')['refund_no'];
        $c = 0;
        do {
            //退款是异步的，防止退款过程中请求此接口，造成返回数据不准确
            $refund = RefundModel::where('order_no',$order_no)->where('refund_no',$refund_no)->first();
            if ($refund && $refund->status === 0) {
                sleep(2);
                $c++;
            } else {
                break;
            }
        } while ($c < 3);

        if ($refund) {
            $items = [];
            $amount = 0;
            foreach ($refund->refundDetail as $detail) {

                $items[] = [
                    'platform' => RefundDetailModel::modifierRefundPlatform($detail->refund_platform),
                    'money' => $detail->refund_amount + 0,
                    'status' => RefundDetailModel::modifierStatus($detail->status),
                    'date' => $detail->updated_at->toDateTimeString(),
                ];

                if ($detail->status === 1) {
                    $amount += $detail->refund_amount;
                }
            }
            $data = [
                'status' => RefundModel::modifierStatus($refund->status),
                'amount' => $amount,
                'items' => $items,
            ];
            return Result::success($data);
        } else {
            return Result::error('单据不存在', -105);
        }

    }
}
